<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Categories extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('category');
	}
	public function ajax_category_get(){
		$categoryEntity =  new CI_Category_entity();
		$category = $categoryEntity->getCategoryByCode($_REQUEST['code']);
		echo json_encode($category);
	}
	public function ajax_category_list(){
		$categoryEntity =  new CI_Category_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$categoryList = $categoryEntity->getCategoryListByLanguage($defaultLanguageId);
		$data['categoryList'] = $categoryList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/categories/index', $data);
	}
	public function ajax_category_save(){
		$categoryNameList = $_REQUEST['category'];
		$categoryNameDefault = '';
		foreach ($categoryNameList as $key => $value) {
			if($value != null && $value != ""){
				$categoryNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($categoryNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_category_require');
		}else{
			if($this->_check_category(0, $categoryNameDefault)){
				$result['result'] = false;
				$result['message'] = lang('escort_category_existed');
			}else{
				$code = 0;
				foreach ($categoryNameList as $key => $value) {
					$categoryEntity =  new CI_Category_entity();
					if($value != null && $value != ''){
						$categoryEntity->setCategory($value);
					}else{
						$categoryEntity->setCategory($categoryNameDefault);
					}
					$categoryEntity->setLanguageId($key);
					$categoryEntity->setCode($code);
					$id = $categoryEntity->save();
					if($code == 0){
						$code = $id;
						$categoryEntity->setId($id);
						$categoryEntity->setCode($code);
						$categoryEntity->update();
					}
				}
				$result['result'] = true;
				$result['message'] =lang('escort_category_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_category_update(){
		$categoryNameList = $_REQUEST['category'];
		$categoryIdList = $_REQUEST['category_id'];
		$categoryCode = $_REQUEST['category_code'];
		$categoryNameDefault = '';
		foreach ($categoryNameList as $key => $value) {
			if($value != null && $value != ""){
				$categoryNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($categoryNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_category_require');
		}else{
			$keys = array_keys($categoryIdList);
			if($this->_check_category($categoryIdList[$keys[0]], $categoryNameDefault)){
				
				$result['result'] = false;
				$result['message'] = lang('escort_category_existed');
			}else{
				foreach ($categoryNameList as $key => $value) {
					$categoryEntity =  new CI_Category_entity();
					if($value != null && $value != ''){
						$categoryEntity->setCategory($value);
					}else{
						$categoryEntity->setCategory($categoryNameDefault);
					}
					$categoryEntity->setLanguageId($key);
					$categoryEntity->setCode($categoryCode);
					$categoryEntity->setId($categoryIdList[$key]);
					$categoryEntity->update();
				}
				$result['result'] = true;
				$result['message'] =lang('escort_category_save_success');
			}
		}
		
		echo json_encode($result);
	}
	public function ajax_category_delete(){
		$result = array();
		$categoryEntity =  new CI_Category_entity();
		$result['result'] = $categoryEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_category_delete_success');
		}else{
			$result['message'] = lang('escort_category_delete_error');
		}
		echo json_encode($result);
	}
	function _check_category($id, $name){
		$ci = &get_instance();
		return $ci->category_entity->checkExistedName($id, $name);
	}
}
?>